<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-del="rowDel">
        <template slot="deploymentId" slot-scope="scope" >
          <el-tag :type="scope.row.deploymentId?'success':''">{{scope.row.deploymentId?"已发布":"未发布"}}</el-tag>
        </template>
        <template slot="menuLeft" slot-scope="scope">
            <el-button @click="handleEdit" type="primary" icon="el-icon-cloudy">在线设计</el-button>
        </template>
        <template  slot="menu" slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)">设计
          </el-button>
          <el-button
            v-if="!scope.row.deploymentId"
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="rowDel(scope.row)"
            >删除
          </el-button>
          <el-button
            v-if="scope.row.deploymentId"
            type="text"
            size="mini"
            icon="el-icon-coin"
          >流程配置
          </el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-search"
          >预览
          </el-button>
          <el-button
            v-if="!scope.row.deploymentId"
            type="text"
            size="mini"
            icon="el-icon-upload"
            @click="deployModel(scope.row)"
          >部署

          </el-button>
          <el-button
            v-if="scope.row.deploymentId"
            type="text"
            size="mini"
          >版本监控
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog
      :visible.sync="dialogEditorVisible"
      top="5px"
      width="98%"
      :before-close="handleClose">
      <div style="height: 85vh" class="bpmn-editor">
        <my-process-designer
          :key="`designer-${reloadIndex}`"
          v-bind="controlForm"
          :value="xmlString"
          keyboard
          ref="processDesigner"
          @element-click="elementClick"
          @init-finished="initModeler"
        />
        <my-properties-panel  :key="`penal-${reloadIndex}`" :bpmn-modeler="modeler" :prefix="controlForm.prefix" class="process-panel" />
        <div class="demo-control-bar">
          <div class="open-control-dialog" @click="controlDrawerVisible = true"><i class="el-icon-setting"></i></div>
        </div>
        <el-drawer  :visible.sync="controlDrawerVisible" size="400px" title="偏好设置" append-to-body destroy-on-close>
          <el-form :model="controlForm" size="small" label-width="100px" class="control-form" @submit.native.prevent>
            <el-form-item label="流程ID">
              <el-input v-model="controlForm.modelKey" @change="reloadProcessDesigner" />
            </el-form-item>
            <el-form-item label="流程名称">
              <el-input v-model="controlForm.modelName" @change="reloadProcessDesigner" />
            </el-form-item>
            <el-form-item label="流转模拟">
              <el-switch v-model="controlForm.simulation" inactive-text="停用" active-text="启用" @change="reloadProcessDesigner" />
            </el-form-item>
            <el-form-item label="禁用双击">
              <el-switch v-model="controlForm.labelEditing" inactive-text="停用" active-text="启用" @change="changeLabelEditingStatus" />
            </el-form-item>
            <el-form-item label="隐藏label">
              <el-switch v-model="controlForm.labelVisible" inactive-text="停用" active-text="启用" @change="changeLabelVisibleStatus" />
            </el-form-item>
            <el-form-item label="流程引擎">
              <el-radio-group v-model="controlForm.prefix" @change="reloadProcessDesigner">
                <el-radio label="camunda">camunda</el-radio>
                <el-radio label="flowable">flowable</el-radio>
                <el-radio label="activiti">activiti</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="工具栏">
              <el-radio-group v-model="controlForm.headerButtonSize">
                <el-radio label="mini">mini</el-radio>
                <el-radio label="small">small</el-radio>
                <el-radio label="medium">medium</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-drawer>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import translations from "@/translations";
  // 自定义渲染（隐藏了 label 标签）
  import CustomRenderer from "@/modules/custom-renderer";
  // 自定义元素选中时的弹出菜单（修改 默认任务 为 用户任务）
  import CustomContentPadProvider from "@/components/BpmnEditor/process-designer/plugins/content-pad";
  // 自定义左侧菜单（修改 默认任务 为 用户任务）
  import CustomPaletteProvider from "@/components/BpmnEditor/process-designer/plugins/palette";
  import {addObj, delObj, deploy,getXml, fetchList} from '@/api/activiti/activiti'
  import {tableOption} from '@/const/crud/activiti/activiti'
  import {mapGetters} from 'vuex'
  export default {
    data() {
      return {
        searchForm: {},
        dialogEditorVisible: false,
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption,
        xmlString: "",
        modeler: null,
        reloadIndex: 0,
        controlDrawerVisible: false,
        translationsSelf: translations,
        controlForm: {
          modelName:"",
          modelKey:"",
          simulation: true,
          labelEditing: false,
          labelVisible: false,
          prefix: "activiti",
          headerButtonSize: "mini",
          additionalModel: [CustomContentPadProvider, CustomPaletteProvider]
        },
        addis: {}
      }
    },
    watch:{



    },

    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      getList(page, params) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.searchForm)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      //编辑流程图
      handleEdit(row){
        if (row.id !== undefined ){
          getXml(row.id).then(res=>{
            this.reloadProcessDesigner()
            this.xmlString = res.data.data
            this.controlForm.modelName = row.name
            this.controlForm.modelKey = row.key
          })
        }else {
          this.reloadProcessDesigner()
          this.controlForm.modelName = ""
          this.controlForm.modelKey = ""
          this.xmlString = ""
        }
        this.dialogEditorVisible = true
      },
      //预览流程图
      handleView(row) {

      },
      //关闭编辑器
      handleClose() {
        this.dialogEditorVisible = false
      },
      //删除模型
      rowDel: function (row) {
        this.$confirm("确认删除?").then(_ =>{
          delObj(row.id).then(res=>{
            this.$message.success(res.data.msg)
            this.refreshChange()
          })
        }).catch(_ => {});
      },
      /**
       * @title 数据添加
       * @param row 为当前的数据
       * @param done 为表单关闭函数
       **/
      searchChange(form, done) {
        this.searchForm = form
        this.getList(this.page, form)
        done()
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      refreshChange() {
        this.getList(this.page)
      },
      initModeler(modeler) {
        setTimeout(() => {
          this.modeler = modeler;
        }, 10);
      },

      reloadProcessDesigner() {
        this.controlForm.additionalModel = [];
        for (let key in this.addis) {
          if (this.addis[key]) {
            this.controlForm.additionalModel.push(this.addis[key]);
          }
        }
        this.reloadIndex += 1;
        this.modeler = null; // 避免 panel 异常
      },
      changeLabelEditingStatus(status) {
        this.addis.labelEditing = status ? { labelEditingProvider: ["value", ""] } : false;
        this.reloadProcessDesigner();
      },
      changeLabelVisibleStatus(status) {
        this.addis.customRenderer = status ? CustomRenderer : false;
        this.reloadProcessDesigner();
      },
      elementClick(element) {
        this.element = element;
      },
      deployModel(row){
        this.$confirm("确定部署？").then(_=>{
          deploy(row.id).then(res=>{
            this.$message.success(res.data.msg)
            this.refreshChange()
          })
        }).catch(_=>{})

      }
    }
  }
</script>
<style scoped lang="scss">
body {
  overflow: hidden;
  margin: 0;
  box-sizing: border-box;
}
.bpmn-editor {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: inline-grid;
  grid-template-columns: auto max-content;
}
.demo-control-bar {
  position: fixed;
  right: 8px;
  bottom: 8px;
  z-index: 1;
  .open-control-dialog {
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    font-size: 32px;
    background: rgba(64, 158, 255, 1);
    color: #ffffff;
    cursor: pointer;
  }
}
.control-form {
  .el-radio {
    width: 100%;
    line-height: 32px;
  }
}
body,
body * {
  /* 滚动条 */
  &::-webkit-scrollbar-track-piece {
    background-color: #fff; /*滚动条的背景颜色*/
    -webkit-border-radius: 0; /*滚动条的圆角宽度*/
  }
  &::-webkit-scrollbar {
    width: 10px; /*滚动条的宽度*/
    height: 8px; /*滚动条的高度*/
  }
  &::-webkit-scrollbar-thumb:vertical {
    /*垂直滚动条的样式*/
    height: 50px;
    background-color: rgba(153, 153, 153, 0.5);
    -webkit-border-radius: 4px;
    outline: 2px solid #fff;
    outline-offset: -2px;
    border: 2px solid #fff;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.3);
    -webkit-border-radius: 4px;
  }
  &::-webkit-scrollbar-thumb:hover {
    /*滚动条的hover样式*/
    background-color: rgba(159, 159, 159, 0.5);
    -webkit-border-radius: 4px;
  }
}
</style>
